package com.sens.utils;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import com.sens.orderback.ui.widget.FloatLayout;


public class FloatWindowManager {
    /**
     * 悬浮窗
     */
    private static FloatLayout floatLayout;
    private static WindowManager windowManager;
    private static WindowManager.LayoutParams windowParams;
    private static boolean hasPermission = false;
    private static Context context;

    /**
     * 创建一个小悬浮窗。初始位置为屏幕的右部中间位置。
     *
     * @param context 必须为应用程序的Context.
     */
    public static void createFloatWindow(Context context,View.OnClickListener listener) {
        context = context.getApplicationContext();
        FloatWindowManager.context = context;
        if (windowParams == null)
            windowParams = new WindowManager.LayoutParams();
        WindowManager windowManager = getWindowManager(context);
        if (floatLayout == null)
            floatLayout = new FloatLayout(context);
        if (Build.VERSION.SDK_INT >= 23) {
            if (Settings.canDrawOverlays(context)) {
                //执行6.0以上绘制代码
                hasPermission = true;
            }
        } else {
            hasPermission = true;
        }
        windowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        //设置图片格式，效果为背景透明
        windowParams.format = PixelFormat.RGBA_8888;
        //设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
        windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        //调整悬浮窗显示的停靠位置为左侧置顶
        windowParams.gravity = Gravity.START | Gravity.TOP;
        DisplayMetrics displayMetrics = new DisplayMetrics();
        //取得窗口属性
        FloatWindowManager.windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        //窗口的宽度
        int screenWidth = displayMetrics.widthPixels;
        //窗口高度
        int screenHeight = displayMetrics.heightPixels;
        //以屏幕左上角为原点，设置x、y初始值，相对于gravity
        windowParams.x = screenWidth;
        windowParams.y = screenHeight;
        floatLayout.setParams(windowParams);
        floatLayout.setOnClickListener(listener);
        if (hasPermission) {
            if (!floatLayout.isAttachedToWindow()) {
                windowManager.addView(floatLayout, windowParams);
            }
        }
    }

    /**
     * 返回当前已创建的WindowManager。
     */
    private static WindowManager getWindowManager(Context context) {
        if (windowManager == null) {
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        return windowManager;
    }

    public static void toogle(boolean isON) {
        if (floatLayout == null) return;
        if (floatLayout.isEnabled() && floatLayout.isClickable())
            floatLayout.toogle(isON);
    }

    protected static void show() {
        if (floatLayout == null || windowManager == null) return;
        if (!hasPermission) {
            if (!hasPermission && Build.VERSION.SDK_INT >= 23) {
                if (Settings.canDrawOverlays(context)) {
                    //执行6.0以上绘制代码
                    hasPermission = true;
                } else {
                    hasPermission = false;
                }
                if (hasPermission && !floatLayout.isAttachedToWindow()) {
                    windowManager.addView(floatLayout, windowParams);
                }
            }
        }
        if (floatLayout.getVisibility() != View.VISIBLE) {
            floatLayout.setVisibility(View.VISIBLE);
        }
    }
}